﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebVideo.Models;
using MvcLibrary.Repository;
using System.IO;
using System.Data.Linq.SqlClient;
namespace WebVideo.Controllers
{
    public class IndividualController : Controller
    {
        //
        // GET: /Individual/
        // trang chu
        public ActionResult Index(string Friend)
        {
            try
            {
                dbVideoDataContext db = new dbVideoDataContext();
                ViewBag.SoBan = db.EntityFriends.Where(g => g.UserName == Request.Cookies["UserId"].Value || g.UserNameFriend == Request.Cookies["UserId"].Value).ToList().Count;
                ViewBag.SoVideo = db.EntityListVideoOfUsers.Where(g => g.UserName == Request.Cookies["UserId"].Value).ToList().Count;
                if (Friend != Request.Cookies["UserId"].Value)
                {
                    ViewBag.SoBan = db.EntityFriends.Where(g => g.UserName == Friend || g.UserNameFriend == Friend).ToList().Count;
                    ViewBag.SoVideo = db.EntityListVideoOfUsers.Where(g => g.UserName == Friend).ToList().Count;
                }

            }
            catch
            {
                ViewBag.SoBan = 0;
                ViewBag.SoVideo = 0;
                return View();
            }

            try
            {

                dbVideoDataContext db = new dbVideoDataContext();
                EntityUser b = db.EntityUsers.Where(g => g.UserName == Request.Cookies["UserId"].Value).First();
                if (Friend != Request.Cookies["UserId"].Value)
                {
                    b = db.EntityUsers.Where(g => g.UserName == Friend).First();
                }
                if (db.EntityFriends.Where(g => (g.UserName == Friend && g.UserNameFriend == Request.Cookies["UserId"].Value) || (g.UserNameFriend == Friend && g.UserName == Request.Cookies["UserId"].Value)).ToList().Count > 0)
                {
                    ViewBag.Friended = Friend;
                }
                else
                {
                    ViewBag.Friended = "";
                }
                ViewBag.Friend = Friend;
                return PartialView(b);
            }
            catch
            {
                return PartialView();
            }
        }
        // Hien thi thong tin ca nhan
        public ActionResult ThongTinCaNhan(string Friend)
        {
            try
            {
                dbVideoDataContext db = new dbVideoDataContext();
                EntityUser b = db.EntityUsers.Where(g => g.UserName == Request.Cookies["UserId"].Value).First();
                if (Friend != Request.Cookies["UserId"].Value)
                {
                    b = db.EntityUsers.Where(g => g.UserName == Friend).First();
                }
                ViewBag.ThongTinCaNhan = Friend;
                return View(b);
            }
            catch
            {
                return View();
            }
        }
        //tuong nha
        public ActionResult Wall(string Friend)
        {
            try
            {
                dbVideoDataContext db = new dbVideoDataContext();
                var query = from p in db.EntitySTT_Comments
                            where (p.UserName == Request.Cookies["UserId"].Value && p.IdRoot == null)
                            orderby p.Date descending
                            select p;
                if (Friend != Request.Cookies["UserId"].Value)
                {
                    query = from p in db.EntitySTT_Comments
                            where (p.UserName == Friend && p.IdRoot == null)
                            orderby p.Date descending
                            select p;
                }
                ViewBag.Wall = Friend;
                return View(query.ToList().Take(30));

            }
            catch
            {
                return ViewBag.NotSTT("hehe");
            }
        }
        public ActionResult _WallFriend()
        {
            try
            {
                dbVideoDataContext db = new dbVideoDataContext();
              
                var query = from p in db.EntitySTT_Comments
                            where (from o in db.EntityFriends where o.UserNameFriend == Request.Cookies["UserId"].Value select o.UserName).Contains(p.UserName) || (from o in db.EntityFriends where o.UserName == Request.Cookies["UserId"].Value select o.UserNameFriend).Contains(p.UserName)
                            orderby p.Date descending
                            select p;
                return View(query.Where(g=>g.UserName!= Request.Cookies["UserId"].Value).ToList().Take(30));

            }
            catch
            {
                return ViewBag.NotSTT("hehe");
            }
        }
        //comment wall
        public ActionResult WallComment(Int64 IdCom, string Comment, Int64 IdDelete, Int64 IdLike)
        {
            try
            {
                dbVideoDataContext db = new dbVideoDataContext();
                if (Comment != "")
                {
                    EntitySTT_Comment stt = new EntitySTT_Comment();
                    stt.IdRoot = IdCom;
                    stt.UserName = Request.Cookies["UserId"].Value;
                    stt.Date = DateTime.Now;
                    stt.Des = Comment;
                    stt.Likes = 0;
                    db.EntitySTT_Comments.InsertOnSubmit(stt);
                    db.SubmitChanges();
                }
                if (IdLike > 0)
                {
                    var query1 = db.EntityDetailComments.Where(g => g.IdComment == IdLike && g.UserName == Request.Cookies["UserId"].Value);
                    if (query1.ToList().Count <= 0)
                    {
                        EntitySTT_Comment cm = db.EntitySTT_Comments.Where(g => g.Id == IdLike).First();
                        cm.Likes = cm.Likes + 1;
                        EntityDetailComment d = new EntityDetailComment();
                        d.UserName = Request.Cookies["UserId"].Value;
                        d.IdComment = IdLike;
                        db.EntityDetailComments.InsertOnSubmit(d);
                    }

                    db.SubmitChanges();
                }
                if (IdDelete > 0)
                {
                    EntitySTT_Comment cm = db.EntitySTT_Comments.Where(g => g.Id == IdDelete).First();
                    db.EntitySTT_Comments.DeleteOnSubmit(cm);
                    db.SubmitChanges();
                }
              
                var query = from p in db.EntitySTT_Comments
                            where p.IdRoot == IdCom
                            orderby p.Date descending
                            select p;
                ViewBag.IdRoot = IdCom;
                return PartialView(query.ToList());
                
            }
            catch
            {
                return ViewBag.NotSTT("hehe");
            }
        }

        // dang trang thai
        public string Think(string think, string UserName)
        {
            try
            {
                dbVideoDataContext db = new dbVideoDataContext();
                EntitySTT_Comment faq = new EntitySTT_Comment();
                faq.UserName = UserName;
                faq.Des = think;
                faq.Date = DateTime.Now;
                faq.Likes = 0;
                db.EntitySTT_Comments.InsertOnSubmit(faq);
                db.SubmitChanges();
                return "Complete";
            }
            catch
            {
                return "Error";
            }
           // return RedirectToAction("Wall", "Individual", new { Friend = System.Web.HttpContext.Current.Request.Cookies["UserId"].Value });
        
        }
        // nut like 
        public string Likes(Int64 Id)
        {
            try
            { 
                dbVideoDataContext db = new dbVideoDataContext();

                 var query1 = db.EntityDetailComments.Where(g => g.IdComment == Id && g.UserName == Request.Cookies["UserId"].Value);
                    if (query1.ToList().Count <= 0)
                    {
                        EntitySTT_Comment cm = db.EntitySTT_Comments.Where(g => g.Id == Id).First();
                        cm.Likes = cm.Likes + 1;
                        EntityDetailComment d = new EntityDetailComment();
                        d.UserName = Request.Cookies["UserId"].Value;
                        d.IdComment = Id;
                        db.EntityDetailComments.InsertOnSubmit(d);
                        db.SubmitChanges();
                        return cm.Likes.ToString();
                    }
                    return "Error";
             
            }
            catch
            {
                return "Error";
             
            }

        }
        public string DeleteStatus(Int64 Id)
        {
            try
            { 
                dbVideoDataContext db = new dbVideoDataContext();
                EntitySTT_Comment cm = db.EntitySTT_Comments.Where(g => g.Id == Id).First();

                db.EntitySTT_Comments.DeleteAllOnSubmit(db.EntitySTT_Comments.Where (g=>g.IdRoot == Id).ToList());
                db.SubmitChanges();
                db.EntitySTT_Comments.DeleteOnSubmit(cm);
                db.SubmitChanges();
                return "Complete";
            }
            catch
            {
                return "Error";
             
            }

        }

        #region "Danh sách bạn bè & tìm kiềm"
        // danh sach ban be
        public ActionResult DanhSachBanBe(string Friend)
        {
            dbVideoDataContext db = new dbVideoDataContext();
            var query = from p in db.EntityFriends
                        where p.UserName == Request.Cookies["UserId"].Value || p.UserNameFriend == Request.Cookies["UserId"].Value
                        select p;
            if (Friend != Request.Cookies["UserId"].Value)
            {
                query = from p in db.EntityFriends
                        where p.UserName == Friend || p.UserNameFriend == Friend
                        select p;
            }
            ViewBag.DanhSachBanBe = Friend;
            return View(query.ToList());
        }

        public ActionResult TimKiemFiend(string search)
        {
            dbVideoDataContext db = new dbVideoDataContext();
            var likestr = string.Format("%{0}%", search);
            var query = db.EntityUsers.Where(x => x.Status == 1 && x.UserName!= Request.Cookies["UserId"].Value &&(SqlMethods.Like(x.Name, likestr) || SqlMethods.Like(x.UserName, likestr)));
            List<LoginModel> ds = new List<LoginModel>();
            foreach(var item in query.ToList() )
            {
                LoginModel lg = new LoginModel();
                lg.Avatar = item.Avatar;
                lg.UserName =item.UserName.ToString();
                lg.dkch = db.EntityFriends.Where(g => (g.UserName == Request.Cookies["UserId"].Value && g.UserNameFriend == item.UserName) || (g.UserNameFriend == Request.Cookies["UserId"].Value && g.UserName == item.UserName)).ToList().Count;
                ds.Add(lg);
            }
            return PartialView(ds);
        }
        public string  KetBan(string UserName)
        {
            try
            {
                dbVideoDataContext db = new dbVideoDataContext();
                EntityFriend friend = new EntityFriend();
                friend.UserName = Request.Cookies["UserId"].Value;
                friend.UserNameFriend = UserName;
                db.EntityFriends.InsertOnSubmit(friend);
                db.SubmitChanges();
                return "Complete";
            }
            catch
            {
                return "Error";
            }
        }
        public string XoaBan(string UserName)
        {
            try
            {
                dbVideoDataContext db = new dbVideoDataContext();
                EntityFriend friend = db.EntityFriends.Where(g => (g.UserName == Request.Cookies["UserId"].Value && g.UserNameFriend == UserName) || (g.UserName == UserName && g.UserNameFriend == Request.Cookies["UserId"].Value)).First();
                db.EntityFriends.DeleteOnSubmit(friend);
                db.SubmitChanges();
                return "Complete";
            }
            catch
            {
                return "Error";
            }
        }
        #endregion

        #region "Chat"
        public ActionResult Chat(string Friend, string FriendChat)
        {
            dbVideoDataContext db = new dbVideoDataContext();
            if (FriendChat != "")
            {
                var query = db.EntityChats
               .Where(k => (k.Friend == Request.Cookies["UserId"].Value && k.UserName == FriendChat) || (k.Friend == FriendChat && k.UserName == Request.Cookies["UserId"].Value))
               .ToList();

                foreach (var item in query)
                {
                    item.Status = 2;
                }
                db.SubmitChanges();
            }
            var q = db.EntityChats
                .Where(k => (k.Friend == Request.Cookies["UserId"].Value && k.UserName == FriendChat) || (k.Friend == FriendChat && k.UserName == Request.Cookies["UserId"].Value))
                .OrderByDescending(g => g.Date)
                .ToList();
            ViewBag.FriendChat = FriendChat;
            return View(q);
        }

        // hiển thị thông báo cho người dùng

        public ActionResult DSTinNhan()
        {
            dbVideoDataContext db = new dbVideoDataContext();
            if (Request.Cookies["UserId"] != null)
            {
                var q = db.EntityChats
                     .Where(k => k.Friend == Request.Cookies["UserId"].Value && k.Status == 1)
                     .GroupBy(a => a.UserName)
                     .Select(g => g.First());

                return PartialView(q.OrderByDescending(g => g.Date).ToList());
            }
            else
            {
                return PartialView(db.EntityChats.Where(g => g.UserName == "").ToList());
            }
        }
        // hiển thị danh sách tin nhắn
        public ActionResult _ListFriendChat()
        {
            dbVideoDataContext db = new dbVideoDataContext();
            
            var q = db.EntityChats
                .Where(k => k.Friend == Request.Cookies["UserId"].Value)
                .GroupBy(a => a.UserName)
                .Select(g => g.First());
            return PartialView(q.OrderByDescending(g=>g.Date).ToList());
        }
        // hiển thị toàn bộ danh sách bạn
        public ActionResult _ListAllFriend()
        {
            dbVideoDataContext db = new dbVideoDataContext();

            var query = from p in db.EntityFriends
                        where p.UserName == Request.Cookies["UserId"].Value || p.UserNameFriend == Request.Cookies["UserId"].Value
                        select p;
            string k = query.ToList().Count.ToString();

            return PartialView(query.ToList());
        }
        // hiển thị nội dung chat
        public ActionResult _ContentChat(string FriendChat, string Content, string Delete)
        {
            dbVideoDataContext db = new dbVideoDataContext();
           // xóa nôi dung chat
            if (Delete != "" && FriendChat == "")
            {
                try
                {
                    IEnumerable<EntityChat> delc = db.EntityChats
                        .Where(k => (k.Friend == Request.Cookies["UserId"].Value && k.UserName == Delete) || (k.Friend == Delete && k.UserName == Request.Cookies["UserId"].Value))
                        .ToList();
                   // string dem = delc.Count().ToString();
                    db.EntityChats.DeleteAllOnSubmit(delc);
                    db.SubmitChanges();
                }
                catch
                {
                }
            }
            // reply tin nhắn
            if (Content != "")
            {
                EntityChat chat = new EntityChat();
                chat.UserName = Request.Cookies["UserId"].Value;
                chat.Friend = FriendChat;
                chat.Contents = Content;
                chat.Date = DateTime.Now;
                chat.Status = 1;
                db.EntityChats.InsertOnSubmit(chat);
                db.SubmitChanges();
            }
                if (FriendChat != "" && Content == "" && Delete=="")
                {
                   var query  = db.EntityChats
                  .Where(k => (k.Friend == Request.Cookies["UserId"].Value && k.UserName == FriendChat) || (k.Friend == FriendChat && k.UserName == Request.Cookies["UserId"].Value))
                  .ToList();

                   foreach (var item in query)
                   {
                       item.Status = 2;
                   }
                   db.SubmitChanges();
                }
            var q = db.EntityChats
                .Where(k => (k.Friend == Request.Cookies["UserId"].Value && k.UserName == FriendChat) || (k.Friend == FriendChat && k.UserName == Request.Cookies["UserId"].Value))
                .OrderByDescending(g=>g.Date)
                .ToList();
            return PartialView(q.Take(200));
        }
      
        #endregion
        // thay doi thong tin ca nhan
        public string ChangeInfo(string Name, int YearBorn, string Sex, string City, string BankAccount, string NameBank)
        {
            try
            {
                dbVideoDataContext db = new dbVideoDataContext();
                EntityUser b = db.EntityUsers.Where(k => k.UserName == Request.Cookies["UserId"].Value).First();

                b.Name = Name;
                b.YearBorn = YearBorn;
                b.Sex = Sex;
                b.City = City;
                b.BankAccount = BankAccount;
                b.NameBank = NameBank;
                db.SubmitChanges();
                return ("Complete");
            }
            catch
            {
                return ("Error");
            }

        }
        //thay doi anh avatar
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult ChangeAvatar(HttpPostedFileBase Image)
        {
            dbVideoDataContext db = new dbVideoDataContext();
            try
            {
                if (Image != null && Image.ContentLength > 0)
                {
                    int kb = Image.ContentLength / 1024; //file size kb
                    if (kb >= 2048) // file qua lon
                    {
                        return RedirectToAction("Index", "NError", new { errorMsg = "File hình ảnh quá lớn" });
                    }

                    ImageHelper imgHelper = new ImageHelper();
                    string encodestring = imgHelper.encodeImageFile(Image);

                    if (encodestring == "!")
                        return RedirectToAction("ThongtinCaNhan", "Individual", new { Friend = System.Web.HttpContext.Current.Request.Cookies["userId"].Value });

                    var path = Path.Combine(Server.MapPath("~/Content/Images/Avatar"), encodestring);
                    Image.SaveAs(path);
                    var query = db.EntityUsers.Where(k => k.UserName == Request.Cookies["UserId"].Value);
                     if (query != null)
                    {
                        EntityUser p = query.First();
                        if (p.Avatar != null)
                        {
                            string fileToDelete = Path.Combine(Server.MapPath("~/Content/Images/Avatar"), p.Avatar); // file hinh cu
                            System.IO.File.Delete(fileToDelete);
                            p.Avatar = encodestring;
                            db.SubmitChanges();
                            Response.Cookies["UserAvatar"].Value = encodestring;
                        }
                        else
                        {
                 
                            p.Avatar = encodestring;
                            db.SubmitChanges();
                            Response.Cookies["UserAvatar"].Value = encodestring;
                        }
                    }


                }

                return RedirectToAction("ThongtinCaNhan", "Individual", new { Friend = System.Web.HttpContext.Current.Request.Cookies["userId"].Value });
            }
            catch
            {

                return RedirectToAction("ThongtinCaNhan", "Individual", new { Friend = System.Web.HttpContext.Current.Request.Cookies["userId"].Value });
            }
        }
        // thay mat khau
        public string ChangePassWord(string PassWord, string NewPass)
        {
            dbVideoDataContext db = new dbVideoDataContext();
            Security ser = new Security();
            PassWord = ser.GetHashPassword(PassWord);
            try
            {
                var query = db.EntityUsers.Where(k => k.UserName == Request.Cookies["UserId"].Value && k.Pass == PassWord);
                if (query != null)
                {
                    EntityUser p = query.First();
                    NewPass = ser.GetHashPassword(NewPass);
                    p.Pass = NewPass;
                }
                db.SubmitChanges();
                return ("Complete");
            }
            catch
            {
                return ("Error");
               
            }
        }

        public ActionResult AddFriend(string Friend)
        {
            dbVideoDataContext db = new dbVideoDataContext();
            var query = from p in db.EntityFriends
                        where p.UserName == Request.Cookies["UserId"].Value
                        select p;
            ViewBag.Link = db.EntityUsers.Where(g => g.UserName == Request.Cookies["UserId"].Value).First().Link;
            ViewBag.AddFriend = Friend;
            return View(query.ToList());
        }
        public ActionResult ThongKe(string Friend)
        {
            dbVideoDataContext db = new dbVideoDataContext();
            var query = from p in db.EntityUsers
                        where p.UserName == Request.Cookies["UserId"].Value
                        select p;
            if (Friend != Request.Cookies["UserId"].Value)
            {
                query = from p in db.EntityUsers
                        where p.UserName == Friend
                        select p;
            }
            ViewBag.ThongKe = Friend;
            return View(query.ToList());
        }
        public ActionResult ThongKeBan(string Friend)
        {
            dbVideoDataContext db = new dbVideoDataContext();
            var query = from p in db.EntityFriends
                        where p.UserName == Request.Cookies["UserId"].Value
                        select p;
            var query1 = from p in db.EntityFriends
                        where p.UserName == Request.Cookies["UserId"].Value || p.UserNameFriend == Request.Cookies["UserId"].Value 
                        select p;
            if (Friend != Request.Cookies["UserId"].Value)
            {
                query = from p in db.EntityFriends
                        where p.UserName == Friend
                        select p;
                query1 = from p in db.EntityFriends
                        where p.UserName == Friend || p.UserNameFriend == Friend
                        select p;
            }
            ViewBag.DSBanCap1 = query.Where(g => g.LevelFriend == 1).ToList().Count();
            ViewBag.DSBanCap2 = query.Where(g => g.LevelFriend == 2).ToList().Count();
            ViewBag.DSBan = query1.ToList().Count();
            
            ViewBag.ThongKeBan = Friend;
            return PartialView();
        }
        public ActionResult ThongKeVideo(string Friend)
        {
            dbVideoDataContext db = new dbVideoDataContext();
            var query = from p in db.EntityListVideoOfUsers
                        where p.UserName == Request.Cookies["UserId"].Value && p.Status == 2
                        select p;
            if (Friend != Request.Cookies["UserId"].Value)
            {
                query = from p in db.EntityListVideoOfUsers
                        where p.UserName == Friend && p.Status == 2
                        select p;
            }
            ViewBag.ThongKeVideo = Friend;
            return PartialView(query.ToList());
        }
        public ActionResult VideoDaXem(string Friend)
        {
            dbVideoDataContext db = new dbVideoDataContext();
            var query = from p in db.EntityListVideoOfUsers
                        where p.UserName == Request.Cookies["UserId"].Value && p.Status == 2
                        select p;
            if (Friend != Request.Cookies["UserId"].Value)
            {
                query = from p in db.EntityListVideoOfUsers
                        where p.UserName == Friend && p.Status == 2
                        select p;
            }
            ViewBag.VideoDaXem = Friend;
            return View(query.ToList());
        }
        #region "Thanh toán"
        public ActionResult Payment(string Friend)
        {
            try
            {

                dbVideoDataContext db = new dbVideoDataContext();
                ViewBag.DiemVideo = db.EntityUsers.Where(g => g.UserName == Request.Cookies["UserId"].Value).First().PointVideo;
                ViewBag.DiemBan1 = db.EntityUsers.Where(g => g.UserName == Request.Cookies["UserId"].Value).First().PointFriend1;
                ViewBag.DiemBan2 = db.EntityUsers.Where(g => g.UserName == Request.Cookies["UserId"].Value).First().PointFriend2;
            }
            catch
            {
                ViewBag.DiemVideo = 0;
                ViewBag.DiemBan2 = 0;
                ViewBag.DiemBan1 = 0;
            }

            try
            {
                dbVideoDataContext db = new dbVideoDataContext();
                var query = from p in db.EntityProduct_Points
                            select p;
                ViewBag.Payment = Friend;
                return View(query.ToList());
            }
            catch
            {
                return View();
            }
        }
        public string DoiTien()
        {
            try
            {
                dbVideoDataContext db = new dbVideoDataContext();
                EntityUser u = db.EntityUsers.Where(g => g.UserName == Request.Cookies["UserId"].Value).First();
                int? TongTien = u.PointVideo + u.PointFriend1 + u.PointFriend2;
                if (TongTien < 100000)
                {
                    return "-2"; // không đủ tiền
                }
                else
                {
                 
                    if (u.BankAccount == null)
                    {
                        return "-1";
                    }
                    else
                    {
                        int? sotien = TongTien / 100000;
                        EntityReport_Point rp = new EntityReport_Point();
                        rp.Date = DateTime.Now;
                        rp.Pointed = 50*sotien;
                        rp.UserName = Request.Cookies["UserId"].Value;
                        rp.Status = 1;
                        db.EntityReport_Points.InsertOnSubmit(rp);

                        int? cl = TongTien - (sotien * 100000);
                        // trừ điểm của user
                        int? tiendadoi =  sotien * 100000;
                        if (u.PointVideo > tiendadoi)
                        {
                            u.PointVideo = u.PointVideo - tiendadoi;
                        }
                        else
                        {
                          
                            tiendadoi = tiendadoi - u.PointVideo;
                            u.PointVideo = 0;
                            if (u.PointFriend1 > tiendadoi)
                            {
                                u.PointFriend1 = u.PointFriend1 - tiendadoi;
                            }
                            else
                            {
                                
                                tiendadoi = tiendadoi - u.PointFriend1;
                                u.PointFriend1 = 0;
                                if (tiendadoi > 0)
                                {
                                    u.PointFriend2 = u.PointFriend2 - tiendadoi;
                                }
                            }
                        }
                        db.SubmitChanges();
                       
                        return cl.ToString();
                    }
                }
            }
            catch
            {
                return "-3";
            }
        }
        public string  DoiSanPham(int Id, string  SDT)
        {
            try
            {
                dbVideoDataContext db = new dbVideoDataContext();
                EntityUser u = db.EntityUsers.Where(g => g.UserName == Request.Cookies["UserId"].Value).First();
                EntityProduct_Point p = db.EntityProduct_Points.Where(g=>g.Id == Id).First();

                int? TongTien = u.PointVideo + u.PointFriend1 + u.PointFriend2;
                int? cl = 0;
                if (TongTien < p.Point)
                {
                    return "-2"; // không đủ tiền
                }
                else
                {
                   
                    int? sotien = p.Point;
                    cl = TongTien - sotien;

                    EntityReport_PoductPoint rp = new EntityReport_PoductPoint();
                    rp.Date = DateTime.Now;
                    rp.IdProduct = Id;
                    rp.PhoneNumber = SDT;
                    rp.Status = 1;
                    rp.UserName = Request.Cookies["UserId"].Value;

                    db.EntityReport_PoductPoints.InsertOnSubmit(rp);

                    // trừ điểm của user
                    int? tiendadoi = sotien;
                    if (u.PointVideo > tiendadoi)
                    {
                        u.PointVideo = u.PointVideo - tiendadoi;
                    }
                    else
                    {
                        tiendadoi = tiendadoi - u.PointVideo;
                        u.PointVideo = 0;
                        if (u.PointFriend1 > tiendadoi)
                        {
                            u.PointFriend1 = u.PointFriend1 - tiendadoi;
                        }
                        else
                        {

                            tiendadoi = tiendadoi - u.PointFriend1;
                            u.PointFriend1 = 0;
                            if (tiendadoi > 0)
                            {
                                u.PointFriend2 = u.PointFriend2 - tiendadoi;
                            }
                        }
                    }    
                      
                        db.SubmitChanges();

                        return cl.ToString();
                }
            }
            catch
            {
                return "-3";
            }
        }
        #endregion

        public ActionResult Diemvideo(string Friend)
        {
            dbVideoDataContext db = new dbVideoDataContext();
            EntityUser b = db.EntityUsers.Where(g => g.UserName == Request.Cookies["UserId"].Value).First();
            return PartialView(b);
        }

    }
}
